import 'dart:io';

import 'package:flutter/material.dart';
import 'package:get/get_state_manager/src/simple/get_view.dart';


abstract class GetWidgetView<T, S> extends GetView<T> {
  GetWidgetView({Key? key}) : super(key: key);

  @override
  String get tag {
    return S.toString();
  }
}

enum SystemOrientations {
  all,
  landscape,
  portrait,
}

abstract class GetPageView<T> extends GetView<T> {
  GetPageView({Key? key, this.canBack = true, this.canBlank = true})
      : super(key: key);

  /// 是否允许返回
  /// 不允许返回的界面按返回键将弹出退出提示
  final bool canBack;
  final bool canBlank;

  bool _willPop(DateTime? lastTime) {
    if (Platform.isAndroid == true) {
      var dateTime = new DateTime.now().millisecondsSinceEpoch;
      String vstime = (dateTime ~/ 1000).toString(); //访问开始时间
      if (lastTime == null ||
          DateTime.now().difference(lastTime) > Duration(seconds: 2)) {
        return false;
      } else {
        // buryingPoint("PageView",vstime,"exit","PressAgain","S001");
        Future.delayed(Duration(milliseconds: 100), () {
          exit(0);
        });
        return false;
      }
    } else {
      return true;
    }
  }

  Widget buildLayout(BuildContext context);

  void onBlankTap(BuildContext context) {
    FocusScopeNode currentFocus = FocusScope.of(context);
    if (!currentFocus.hasPrimaryFocus && currentFocus.focusedChild != null) {
      FocusManager.instance.primaryFocus?.unfocus();
    }
  }

  @override
  Widget build(BuildContext context) {
    /// 点击空白区域自动隐藏键盘
    Widget child;
    if (canBlank == true)
      child = GestureDetector(
        onTap: () => onBlankTap(context),
        child: buildLayout(context),
      );
    else
      child = buildLayout(context);

    if (canBack == false) {
      /// 当不允许返回时按返回键将提示退出APP,仅支持Android
      DateTime? _lastTime;
      return WillPopScope(
        child: child,
        onWillPop: () async {
          Object con = controller as Object;
          // if (con is WillBackMixin) {
          //   return await con.appWillBackCallback(context);
          // }
          var result = _willPop(_lastTime);
          if (result == false) {
            _lastTime = DateTime.now();
            // Toast.toast(context,
            //     msg: '再按一次退出应用程序'.tr, position: ToastPostion.bottom);
          }
          return result;
        },
      );
    }
    return child;
  }
}
